#include <gtest/gtest.h>
#include "EndpointStore.h"

// 声明 stub 里的计数获取函数
int GetEndpointCreateCount();
int GetEndpointDestroyCount();
int GetChannelCreateCount();
int GetChannelDestroyCount();

TEST(EndpointStoreTest, CreateAndDestroyEndpoint)
{
    hixl::EndpointStore store;

    hccl::EndPoint ep;
    ep.protocol = hccl::CommProtocol::TCP;
    ep.commAddr.type = hccl::CommAddrType::IP_V4;
    ep.commAddr.id = 0;

    hixl::EndpointHandle handle = nullptr;

    int create_before = GetEndpointCreateCount();
    int destroy_before = GetEndpointDestroyCount();

    // CreateEndpoint
    auto ret = store.CreateEndpoint(ep, &handle);
    EXPECT_EQ(ret, hccl::HcclResult::SUCCESS);
    EXPECT_NE(handle, nullptr);
    EXPECT_EQ(GetEndpointCreateCount(), create_before + 1);

    // DestroyEndpoint
    ret = store.DestroyEndpoint(handle);
    EXPECT_EQ(ret, hccl::HcclResult::SUCCESS);
    EXPECT_EQ(GetEndpointDestroyCount(), destroy_before + 1);
}

TEST(EndpointStoreTest, DestroyEndpointNullShouldFail)
{
    hixl::EndpointStore store;
    auto ret = store.DestroyEndpoint(nullptr);
    EXPECT_EQ(ret, hccl::HcclResult::E_PARA);
}

TEST(EndpointStoreTest, DestroyEndpointTwiceShouldFailSecondTime)
{
    hixl::EndpointStore store;

    hccl::EndPoint ep;
    ep.protocol = hccl::CommProtocol::TCP;
    ep.commAddr.type = hccl::CommAddrType::IP_V4;
    ep.commAddr.id = 0;

    hixl::EndpointHandle handle = nullptr;
    auto ret = store.CreateEndpoint(ep, &handle);
    ASSERT_EQ(ret, hccl::HcclResult::SUCCESS);

    ret = store.DestroyEndpoint(handle);
    EXPECT_EQ(ret, hccl::HcclResult::SUCCESS);

    // 再 destroy 一次，因为 endpoints_info_ 已删掉，应返回 E_PARA
    ret = store.DestroyEndpoint(handle);
    EXPECT_EQ(ret, hccl::HcclResult::E_PARA);
}
